package gstudio.views;

import java.io.File;
import java.util.List;

import gstudio.data.IData;
import gstudio.data.RuntimeDataManager;
import gstudio.format.StyleManager;
import gstudio.mp3.MiniPlayerComposite;
import gstudio.ui.ListViewerFabrik;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.part.ViewPart;

public class Artikel_Satz extends ViewPart implements IPropertyChangeListener {
	private ListViewer listViewer;
	private Composite mainComp;
	private MiniPlayerComposite miniPlayer;
	Label mp3Label;
	private IData currentSatz;
	private StyledText styledText;

	@Override
	public void createPartControl(Composite parent) {
		mainComp = new Composite(parent, SWT.BORDER);
		mainComp.setLayout(new GridLayout());
		PropertyListenerManager.getInstance().register(this);
		SashForm sashForm = new SashForm(mainComp, SWT.VERTICAL);
		sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));

		listViewer = new ListViewerFabrik(sashForm, SWT.V_SCROLL)
				.getListViewer();

		FontData[] fontData = new FontData[] { new FontData("Arial", 11,
				SWT.NORMAL) };
		listViewer.getList().setFont(new Font(null, fontData));

		listViewer.addDoubleClickListener(new IDoubleClickListener() {

			@Override
			public void doubleClick(DoubleClickEvent arg0) {
				IStructuredSelection structuredSelection = (IStructuredSelection) arg0
						.getSelection();
				Object obj = structuredSelection.getFirstElement();
				if (obj != null) {
					currentSatz = (IData) obj;
					resetMp3CompStatus();
					miniPlayer.doPlay();
				}

			}
		});

		listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent arg0) {
				IStructuredSelection structuredSelection = (IStructuredSelection) arg0
						.getSelection();
				Object obj = structuredSelection.getFirstElement();
				if (obj != null) {
					currentSatz = (IData) obj;
					StyleManager.getInstance().setStyledText(styledText,
							currentSatz.getValueOf("content"));
				}
			}
		});

		setStyleTextControl(sashForm);
		sashForm.setWeights(new int[] { 8, 2 });
		StyleManager.getInstance().setEditorToolBar(this, styledText);
		setFootComp(mainComp);
		showSatzList(RuntimeDataManager.getInstance().getCurrentData());

	}

	public void propertyChange(PropertyChangeEvent event) {
		Object obj = event.getNewValue();
		if (obj != null && obj instanceof IData) {
			IData artikel = (IData) obj;
			showSatzList(artikel);
		}
	}

	private void showSatzList(IData artikel) {
		if (artikel == null)
			return;

		String ids = artikel.getValueOf("cids").trim();
		if (!ids.equals("")) {
			List<IData> input = RuntimeDataManager.getInstance().getListByIds(
					"satz", ids);
			listViewer.setInput(input);
		} else {
			listViewer.setInput(null);
		}

	}

	private String getMp3File(boolean absolutePath) {
		if (currentSatz == null)
			return "";

		String fileName = currentSatz.getValueOf("mp3").trim();
		File file = new File(fileName);
		if (file.exists() && file.canRead()) {
			if (!absolutePath) {
				return file.getName();
			} else {
				return file.getAbsolutePath();
			}
		}

		return "";
	}

	private void setFootComp(Composite parent) {
		Composite footComp = new Composite(parent, SWT.NONE);
		{
			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
			footComp.setLayoutData(gd);
			GridLayout gl = new GridLayout(5, false);
			gl.marginHeight = 0;
			gl.marginWidth = 0;
			footComp.setLayout(gl);
		}

		// 播放器
		miniPlayer = new MiniPlayerComposite(footComp, SWT.NONE);
		miniPlayer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		resetMp3CompStatus();
		mp3Label = new Label(footComp, SWT.NONE);

	}

	@Override
	public void setFocus() {

	}

	/*
	 * 点击文章列表，mp3不应该终止，而是继续播放，除非刷新
	 */
	private void resetMp3CompStatus() {
		if (currentSatz != null) {
			if (miniPlayer != null) {
				miniPlayer.doStop();
			}
			String fileName = getMp3File(true);
			if (fileName != null) {
				miniPlayer.getPlayButton().setEnabled(true);
				miniPlayer.setInput(fileName, false);
				miniPlayer.run();
			} else {
				miniPlayer.getPlayButton().setEnabled(false);
			}
		}
	}

	private void setStyleTextControl(Composite sashForm) {
		styledText = StyleManager.getInstance().getStyleTextControl(sashForm);
		styledText.addListener(SWT.FocusOut, new Listener() {
			@Override
			public void handleEvent(Event e) {
				viewDoSave();
			}
		});
	}

	protected void viewDoSave() {
		StyleManager.getInstance().styledTextSaveToIData(currentSatz,
				styledText);
	}

}
